﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Twipler.App.DataDoc
{
    public class DataDocValue
    {
        public string Name;
        public object Value;

        public DataDocValue(string name, object value)
        {
            Name = name;
            Value = value;
        }

        public bool ValueToDateTime(out DateTime datetime)
        {
            datetime = DateTime.MinValue;

            string dt = Value.ToString();
            if (string.IsNullOrEmpty(dt))
                return false;

            // Whoever wrote the twitter date serializer should be shot.
            string expectFormat = "ddd MMM dd HH:mm:ss +0000 yyyy";
            if (dt.Length == 31)
                expectFormat = "ddd, dd MMM yyyy HH:mm:ss +0000" ;
            else
            {
                if (dt.Length == 25)
                {
                    if ((dt.IndexOf("Dec") != -1) && (DateTime.Now.Month == 1))
                        dt += (DateTime.Now.Year -1).ToString();
                    else if ((dt.IndexOf("Jan") != -1) && (DateTime.Now.Month == 12))
                        dt += (DateTime.Now.Year + 1).ToString();
                    else
                        dt += (DateTime.Now.Year).ToString();
                }

                if (dt.Length != 30)
                    return false;
            }

            try
            {
                //Assume format: Fri Dec 04 17:18:22 +0000 2009
                datetime =  DateTime.ParseExact(dt, expectFormat, CultureInfo.InvariantCulture);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

		public override string ToString()
		{
			if (Value == null)
				return Name + ": null " + Environment.NewLine;
			
			if (Value == typeof(DataDocValue))
				return " { Name : " + Environment.NewLine + ((DataDocValue)Value) + " }" + Environment.NewLine;

			if (Value.GetType().IsAssignableFrom(typeof(List<DataDocValue>)))
			{
				List<DataDocValue> lv = (List<DataDocValue>)Value;

				StringBuilder x = new StringBuilder();
				x.Append("[ " + Environment.NewLine);

				foreach (DataDocValue val in lv)
					x.AppendFormat("{0}, " + Environment.NewLine, val);

				x.Append("] " + Environment.NewLine);

				return x.ToString();
			}

			return Name + " : " + Value;
		}


        public void DebugRender(StringBuilder html, int indent)
        {
            html.AppendFormat("<span style=\"padding-left:{0}px; color:#666;\">{1}</span>", indent * 12, Name);

            if (Value == null)
            {
                html.Append(" <span style=\"color: #000;\">[null]</span><br/>");
            }
            else if (Value == typeof(DataDocValue))
            {
                html.AppendFormat("(DataDocValue)<br/>");
                ((DataDocValue)Value).DebugRender(html, indent + 1);
            }
            else if (Value.GetType().IsAssignableFrom(typeof(List<DataDocValue>)))
            {
                List<DataDocValue> lv = (List<DataDocValue>)Value;

                html.Append("<br/>");
                foreach (DataDocValue val in lv)
                    val.DebugRender(html, indent + 1);
            }
            else
            {
                html.AppendFormat("<span style=\"color: #000;\">[{0}]</span><br/>", Value);
            }
        }
    

    }
}
